package business;

import data.DBAccess;
import java.util.Arrays;

/**
 *
 * @author Administrator
 */
public class Calc {
    //Defining the coordinates where Scandinavien is located
    static final double lowerLat = 53.63;
    static final double lowerLong = -0.88;
    static final double upperLat = 64.1;
    static final double upperLong = 19.58;
    static final double lat = upperLat - lowerLat;
    static final double longt = upperLong - lowerLong;
    
    public static int[][] shrinkSquare2DArray(int[][] original, int newSize) {
        //Defining the ratio for the original data and a new array
        int ratio = original.length/newSize;
        if(original.length > newSize && ratio < 2)
            ratio = 2;
        int[][] newArray = new int[newSize][newSize];
        //Passing through the 2D-array the insert the info from the database
        for(int m = 0; m < newArray.length; m++) {
            for(int n = 0; n < newArray.length; n++) {
                for(int i = m*ratio; i < m*ratio+ratio; i++) {
                    for(int l = n*ratio; l < n*ratio+ratio; l++) {
                        if(i < original.length && l < original.length)
                            newArray[m][n] += original[i][l];
                    }
                }
            }
        }
        return newArray;
    }
    
    public static int findMax(int[][] array) {
        int result = 0;
        for(int[] arr : array) {
            for(int i : arr) {
                if(i > result) {
                    result = i;
                }
            }
        }
        return result;
    }
    
    public static int[][] makeArray(double[][] array, int size) {
        int[][] newArray = new int[size][size];
        //Setting all points in the array to 1, to avoid problems with WebGL
        for(int i = 0; i < size; i++)
            for(int l = 0; l < size; l++)
                newArray[i][l] = 1;
        for(double[] coor : array) {
            double x = coor[0];
            double y = coor[1];
            if(x < lowerLat || y < lowerLong) {
                
            } else {
                //Finding where the previously defined x and y belong in the 
                //new array
                int i = (int) (size - Math.round(((x - lowerLat)/lat) * size));
                if(i >= size)
                    i = size-1;
                int l = (int) Math.round(((y - lowerLong)/longt) * size);
                if(l >= size)
                    l = size-1;
                newArray[i][l]++;
            }
        }
        return newArray;
    }
}
